package com.liuxu.topnews.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.EditText;
import android.widget.Switch;

import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.bean.App;

import java.util.List;

import static com.liuxu.topnews.MyApplication.checklist;
import static com.liuxu.topnews.MyApplication.float_window;
import static com.liuxu.topnews.MyApplication.globalapps;
import static com.liuxu.topnews.MyApplication.random_read;
import static com.liuxu.topnews.MyApplication.sleep;
import static com.liuxu.topnews.MyApplication.video_read;
import static com.liuxu.topnews.MyApplication.autoclear;

public class ShareUtil {
    private static ShareUtil Instance;
    public static ShareUtil getInstance() {
        if (Instance == null) {
            Instance = new ShareUtil();
        }
        return Instance;
    }
    public void tokenSave(String token) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();//获取到edit对象
        edit.putString("token", token); //通过editor对象写入数据
        edit.commit();//提交数据存入到xml文件中
    }
    public void tokenSet(EditText editText) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        String value = sp.getString("token", "");
        if (!value.equals("")) {
            editText.setText(value);
        }
    }

    public void swithcSet(Switch switch1, Switch switch2, Switch switch3, Switch switch4, Switch switch5) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        switch1.setChecked(sp.getBoolean("switch1", false));
        switch2.setChecked(sp.getBoolean("switch2", false));
        switch3.setChecked(sp.getBoolean("switch3", false));
        switch4.setChecked(sp.getBoolean("switch4", false));
        switch5.setChecked(sp.getBoolean("switch5", false));
        sleep=switch1.isChecked();
        autoclear=switch2.isChecked();
        random_read=switch3.isChecked();
        video_read=switch4.isChecked();
        float_window=switch5.isChecked();

    }
    public void swithcSave(Switch switch1, Switch switch2, Switch switch3, Switch switch4, Switch switch5) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putBoolean("switch1", switch1.isChecked());
        edit.putBoolean("switch2", switch2.isChecked());
        edit.putBoolean("switch3", switch3.isChecked());
        edit.putBoolean("switch4", switch4.isChecked());
        edit.putBoolean("switch5", switch5.isChecked());
        edit.commit();
    }

    public void CheckSave() {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("checkbox", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();//获取到edit对象
        for(int i=0;i<globalapps.size();i++){
            edit.putBoolean(globalapps.get(i).zname,globalapps.get(i).IsCheck);
//            Log.v("保存"+globalapps.get(i).zname,String.valueOf(globalapps.get(i).IsCheck));
        }
        edit.commit();//提交数据存入到xml文件中
    }
//    public void CheckSave() {
//        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("checkbox", Context.MODE_PRIVATE);
//        SharedPreferences.Editor edit = sp.edit();//获取到edit对象
//        for (App app: globalapps) {
//            edit.putBoolean(app.zname, app.IsCheck);
//        }
//        edit.commit();//提交数据存入到xml文件中
//    }

    public boolean GetCheckBox(String zname) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("checkbox", Context.MODE_PRIVATE);
        return sp.getBoolean(zname, false);

    }

    public void AppSet(App app) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("readdata", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        String zname=app.getZname();
        String ename=app.getEname();
        int readcount=sp.getInt(zname,0);
        long usetime=sp.getLong(ename,0L);
        edit.putInt(zname,readcount+app.ReadCount);
        edit.putLong(ename,usetime+app.UseTime);
        edit.commit();
    }
    public String AppUseTimeGet(App app) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("readdata", Context.MODE_PRIVATE);
        String ename=app.getEname();
        long usetime=sp.getLong(ename,0L);
        usetime /= 1000L;
        long hour = usetime / 3600L;
        long minute = usetime / 60L - hour * 60L;
        return hour + "时" + minute + "分" + (usetime - hour * 60L * 60L - minute * 60L) + "秒";
    }
    public int AppReadCountGet(App app) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("readdata", Context.MODE_PRIVATE);
        String zname=app.getZname();
        int readcount=sp.getInt(zname,0);
        return readcount;
    }
    public int CurrentAppIndexGet(App app) {
        SharedPreferences sp = MyApplication.getContext().getSharedPreferences("data", Context.MODE_PRIVATE);
        int index=sp.getInt("CurrentApp",0);
        return index;
    }
    public void celardata(List<App> apps){SharedPreferences sp = MyApplication.getContext().getSharedPreferences("readdata", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        for (int i = 0; i < apps.size(); i++) {
            edit.putInt(apps.get(i).zname,0);
            edit.putLong(apps.get(i).ename,0L);
        }
        edit.commit();
    }
}

